package com.jinzhu.controller;


import com.jinzhu.bo.SubmitOrderBO;
import com.jinzhu.controller.payment.BaseController;
import com.jinzhu.emums.PayMethod;
import com.jinzhu.service.OrderService;
import com.jinzhu.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Api(value = "订单相关", tags = {"订单相关的api接口"})
@RequestMapping("orders")
@RestController
@Slf4j
public class OrdersController extends BaseController {


    /**
     * 订单service
     */
    @Autowired
    private OrderService orderService;

    /**
     * Redis 存储订单
     */
    @Autowired
    private RestTemplate restTemplate;


    /**
     * @param submitOrderBO
     * @param request
     * @param response
     * @return
     * @date 2021-06-24
     */
    @ApiOperation(value = "用户下单", notes = "用户下单", httpMethod = "POST")
    @PostMapping("/create")
    public IMOOCJSONResult create(@RequestBody SubmitOrderBO submitOrderBO,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

        log.info("订单信息：{}", submitOrderBO.toString());
        //1.判断是否是微信或者支付宝支付
        if (!Objects.equals(PayMethod.WEIXIN, submitOrderBO.getPayMethod())
                && !Objects.equals(PayMethod.ALIPAY, submitOrderBO.getPayMethod())) {

            return IMOOCJSONResult.errorMsg("支付方式不正确");
        }


        //2.创建订单
        orderService.createOrder(submitOrderBO);
        //3.创建订单后，移除购物车已经提交的商品

        //4.向支付发送当前订单，用来保存支付中心的订单数据
        return IMOOCJSONResult.ok();
    }

}
